<?php

namespace App\Http\Controllers;

use Log;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use App\Libs\XiaoiBot\XiaoiBot;
use EasyWeChat\OfficialAccount\Application;

/**
 * ClassName: WeChatController
 * @Author: wuqin
 * @DateTime: 2018-01-22 9:25
 * @package App\Http\Controllers
 */
class WeChatController extends Controller
{

    /**
     * 处理微信的请求消息
     *
     * @return string
     */
    public function serve()
    {
        Log::info('request arrived.'); # 注意：Log 为 Laravel 组件，所以它记的日志去 Laravel 日志看，而不是 EasyWeChat 日志

        $app = app('wechat.official_account.default');
        $app->server->push(function ($message) {
            // Log::info('request arrived.', gettype($message));
            //获取到用户发送的文本内容
            $content = $message['Content'];
            $openid = $message['FromUserName'];
            // Log::info('-----2-----.');
            Log::info('-----'.$content.'-----.');
            return $this->get_reply($content,$openid);
            // return "欢迎关注 yiranzai！\n我的博客http://blog.yiranzai.cn \n http://shop.yiranzai.cn/user";
        });

        return $app->server->serve();
    }

    /**
     * Function: serve_yiranzai
     * @Author: wuqin
     * @DateTime: 2018-01-22 9:25
     * @return mixed
     */
    public function serve_yiranzai()
    {
        Log::info('request arrived.'); # 注意：Log 为 Laravel 组件，所以它记的日志去 Laravel 日志看，而不是 EasyWeChat 日志

        $app = app('wechat.official_account.yiranzai');
        $app->server->push(function ($message) {
            // Log::info('request arrived.', gettype($message));
            //获取到用户发送的文本内容
            $content = $message['Content'];
            $openid = $message['FromUserName'];
            // Log::info('-----2-----.');
            Log::info('-----'.$content.'-----.');
            return $this->get_reply($content,$openid);
            // return "欢迎关注 yiranzai！\n我的博客http://blog.yiranzai.cn \n http://shop.yiranzai.cn/user";
        });

        return $app->server->serve();
    }

    /**
     * Function: get_reply
     * @Author: wuqin
     * @DateTime: 2018-01-22 9:25
     * @param $content
     * @param $user_id
     * @return mixed
     */
    public function get_reply($content, $user_id)
    {
        // Log::info('-----'.$content.'-----.');
//        $askResult = $this->xiaoi($content,$user_id);
//        Log::info('-----'.$askResult[1].'-----.');
//        if($askResult[0] != 200 || $askResult[1] == '未识别'){
            $content = json_decode($this->tuling($content, $user_id),true);
            Log::info('-----'.$content['text'].'-----.');
            return $content['text'];
//        }else{
//            // Log::info('-----'.$askResult[1].'-----.');
//            return $askResult[1];
//        }
    }

    /**
     * Function: tuling
     * @Author: wuqin
     * @DateTime: 2018-01-22 9:25
     * @param $content
     * @param $user_id
     * @return string
     */
    public function tuling($content, $user_id)
    {
        $client = new Client();
        // $request = new Request('post', config("tuling.api"));
        $body = array(
            'key' => config("tuling.apikey"),
            'info' => $content,
            'userid' => $user_id,
        );
        $response = $client->request('POST', config("tuling.api"), [
            'body' => json_encode($body)
        ]);
        return $response->getBody()->getContents();
    }

    /**
     * Function: xiaoi
     * @Author: wuqin
     * @DateTime: 2018-01-22 9:25
     * @param $content
     * @param $user_id
     * @return array
     */
    public function xiaoi($content, $user_id)
    {
        //初始化
        $bot = new XiaoiBot( [ 'app_key' => config("xiaoi.apikey"), 'app_secret' => config("xiaoi.apiSecret") ] );
        /**
         * 文字识别,这一块由于返回值和种类偏多,最好还是看一下线上的说明
         */
        return $bot->ask($content,$user_id);
        
    }

    /**
     * Function: demo
     * @Author: wuqin
     * @DateTime: 2018-01-22 9:25
     * @param Application $officialAccount
     */
    public function demo(Application $officialAccount)
    {
        // $officialAccount 则为容器中 EasyWeChat\OfficialAccount\Application 的实例
    }
}
